package com.smart.car.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.member.entity.MemberCard;
import com.smart.car.member.entity.MemberCardExample;
import com.smart.car.member.entity.MemberVehicle;
import com.smart.car.member.mapper.MemberCardMapper;
import com.smart.car.member.service.MemberCardService;
import com.smart.car.member.service.MemberVehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: TODO
 * @author: zoutao
 * @date: 2021/4/15
 */
@Slf4j
@Service
public class MemberCardServiceImpl implements MemberCardService {

    @Autowired
    private MemberCardMapper memberMapper;

    @Override
    public List<MemberCard> list() {
        MemberCardExample carExample = new MemberCardExample();
        List<MemberCard> members = memberMapper.selectByExample(carExample);
        return members;
    }

    @Autowired
    private MemberVehicleService vehicleService;

    @Autowired
    private MemberCardMapper cardMapper;

    @Override
    public int addMonthCard(String json) throws BusinessException{
        MemberCard card = JSONObject.parseObject(json, MemberCard.class);
        return cardMapper.insertSelective(card);
    }

    @Override
    public boolean isMonthCardUser(String vehicleNo) throws BusinessException{
        MemberVehicle vehicle = vehicleService.getVehicle(vehicleNo);
        MemberCardExample example = new MemberCardExample();
        example.createCriteria().andMemberIdEqualTo(vehicle.getMemberId());
        List<MemberCard> cards = cardMapper.selectByExample(example);
        if (ObjectUtil.isEmpty(cards)) {
            log.warn("车牌号：" + vehicle + " ,不是会员用户。");
            return false;
        } else {
            return true;
        }

    }


}